require(["common","jQuery","coAjax","queryString","hash_routie"],function(jSouper,$,coAjax,QueryString,hash_routie){
	//清空缓存
	hash_routie.on("shelves",function () {
		// console.log("???Xx");
		App.set("$Cache.new_goods",{});
		_clear_detail_img_urls();
	})
	coAjax.get(appConfig.bus.tags,{
		bus_id:appConfig.bus_id
	},function (result) {
		App.set("$Cache.shelves.bus_tags",result.result);
	});
	App.set("$Event.tag.add_new_tag",function () {
		var new_tag_name = $.trim(App.get("$Cache.tag.new_tag_name"));
		if (new_tag_name) {
			coAjax.post(appConfig.bus.add_tag,{
				tag_name:new_tag_name
			},function (result) {
				var bus_tags = App.get("$Cache.shelves.bus_tags")||[];
				bus_tags.unshift(result.result);
				App.set("$Cache.shelves.bus_tags",bus_tags);
			},function (errorCode,xhr,errorMsg) {
				alert("error",errorMsg)
			});
		}
		App.set("$Cache.tag.new_tag_name","");
	});
	App.set("$Event.tag.use_tag",function (e,vm) {
		vm.set("_checked",true);
	});
	App.set("$Event.tag.unuse_tag",function (e,vm) {
		vm.set("_checked",false);
	});
	function _clear_detail_img_urls () {
		var _detail_img_urls = [];
		_detail_img_urls.length = 5;
		App.set("$Cache.shelves.detail_img_urls",_detail_img_urls)
	}

	//添加订单
	function _lock_post(){
		alert("info","新商品创建中")
		App.set("$Cache.shelves.lock_submit_button",true);}
	function _unlock_post(){
		App.set("$Cache.shelves.lock_submit_button",false);}

	//正式上传
	function _upload_goods(){
		var new_goods_info = App.get("$Cache.new_goods");
		var tags = [];
		//提取选中的tags
		var bus_tags = App.get("$Cache.shelves.bus_tags")||[];
		bus_tags.forEach(function (tag) {
			if (tag._checked) {
				tags.push(tag._id);
			}
		});
		new_goods_info.tags = tags;
		//提取有效的详情图
		var _detail_img_urls = App.get("$Cache.shelves.detail_img_urls");
		var detail_img_urls = [];
		_detail_img_urls.forEach(function (detail_img) {
			if (detail_img&&detail_img.url) {
				detail_img_urls.push(detail_img.url);
			}
		});
		new_goods_info.detail_img_urls = detail_img_urls;


		coAjax.post(appConfig.bus.post_goods,new_goods_info,function (result) {
			_unlock_post();
			// App.set("$Cache.shelves.upload_info","商品添加成功");
			alert("success","商品添加成功")
			//解锁提交按钮，清空表单
			App.set("$Cache.new_goods",{});
			_clear_detail_img_urls();
			_unlock_post();
		},function (errorCode,xhr,errorMsg) {
			_unlock_post();
			switch(errorCode){
				case "00068":
					alert("error","还未输入“标题”");
					break;
				case "00012":
					alert("error","请先填写表单");
					break;
			}
			alert("error",errorMsg)
			_unlock_post();
		});
	};
	App.set("$Event.shelves.post_new_goods",function (e) {
		//锁定提交按钮，避免再次提交
		_lock_post();
		var preview_img_url = App.get("$Cache.new_goods.preview_img_url")
		//校验相关数据
		if (!preview_img_url) {
			//如果在上传中，等待上传完成
			if (App.get("$Cache.shelves.preview_img_url_upload_status")) {
				// var _wait = setInterval(function () {
				// 	if (!App.get("$Cache.shelves.preview_img_url_upload_status")) {
				// 		clearInterval(_wait);
				// 		_upload_goods();
				// 	}
				// },200);
				alert("warn","请等待商品封面图片上传完成");
				_unlock_post();
			}else{
				alert("warn","请上传商品封面图片");
				_unlock_post();
			}
			return;
		}
		var _detail_img_urls = App.get("$Cache.shelves.detail_img_urls");
		var _warn_str;
		_detail_img_urls.forEach(function (detail_img) {
			if (detail_img.status) {
				_warn_str = "请等待商品详情图上传完成";
			}
		});
		if (_warn_str) {
			alert("warn",_warn_str);
			_unlock_post();
			return;
		}

		//检查通过，上传商品
		_upload_goods();
	});

});

